import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/*
题目描述：从上到下打印二叉树
方法：
使用队列存储每行的结点，依次打印
 */
public class E32_1 {

    public static void main(String[] args) {
        TreeNode1 root = new TreeNode1(1);
        root.left = new TreeNode1(2);
        root.right = new TreeNode1(3);
        root.left.left = new TreeNode1(4);
        root.left.right = new TreeNode1(5);
        root.right.left = new TreeNode1(6);
        root.right.right = new TreeNode1(7);

        ArrayList<Integer> res = PrintFromTopToBottom(root);

        System.out.println(res);
    }

    public static ArrayList<Integer> PrintFromTopToBottom(TreeNode1 root) {
        ArrayList<Integer> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Queue<TreeNode1> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode1 node = queue.poll();
            res.add(node.val);
            if(node.left != null){
                queue.add(node.left);
            }
            if(node.right != null){
                queue.add(node.right);
            }
        }
        return res;
    }
}
class TreeNode1{
    int val;
    TreeNode1 left;
    TreeNode1 right;
    TreeNode1(int val){
        this.val = val;
    }
}
/*
[1, 2, 3, 4, 5, 6, 7]
 */